//
//  SelecteStyleOfCookingController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/24.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "SelecteStyleOfCookingController.h"

@interface SelecteStyleOfCookingController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/**<#属性#>*/
@property (nonatomic,strong)UICollectionView*listView;
/**<#属性#>*/
@property (nonatomic,strong) UICollectionViewFlowLayout * layout;
/**数据源*/
@property (nonatomic,strong)NSMutableArray*dataArray;
/**<#属性#>*/
@property (nonatomic,strong)UIButton*rightBtn;
/**选中的菜系*/
@property (nonatomic,strong)NSString*selecteTitle;

@end

@implementation SelecteStyleOfCookingController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTextField) name:UITextFieldTextDidChangeNotification object:nil];

}
-(void)createUI{
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
    self.title = @"菜系";
    [self.view addSubview:self.listView];
}

-(void)changeTextField{
//    if (self.textField.text.length > 0 ) {
//        self.addBtn.enabled = NO;
//    }else{
//        self.addBtn.enabled = YES;
//    }
}
#pragma mark ---UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return self.dataArray.count;
    }
    return 1;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReusableView" forIndexPath:indexPath];

    if (indexPath.section == 0) {
        UILabel * label = UILabel.new;
        label.text = @"选择菜系";
        label.textColor = UIColorFromRGB(0x454F44);
        label.font = SYSTEMFONT(14);
        [headerView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.centerY.mas_equalTo(0);
        }];
    }
    
    return headerView;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SelecteStyleOfCookingItem * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(SelecteStyleOfCookingItem.class) forIndexPath:indexPath];
    SelecteStyleOfCookingTextFileItem * Onecell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(SelecteStyleOfCookingTextFileItem.class) forIndexPath:indexPath];

    if (indexPath.section == 0) {
        cell.titleName = self.dataArray[indexPath.row];
        return cell;
    }
    
    return Onecell;
}
#pragma mark  定义每个cell的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        CGFloat width = (KScreenWidth - 56)/4;
        return CGSizeMake(width, 36);
    }
    return CGSizeMake(KScreenWidth, 40);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return CGSizeMake( KScreenWidth, 60);
    }
    return CGSizeMake( KScreenWidth, 90);
}
// 列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
//// 行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.selecteTitle = self.dataArray[indexPath.row];
}
#pragma mark ---懒加载
-(UICollectionView *)listView{
    if (!_listView) {
        self.layout= [[UICollectionViewFlowLayout alloc] init];
        _listView = [[UICollectionView alloc] initWithFrame:CGRectMake(16, 0, KScreenWidth - 32 , KScreenHeight-K_TopHeight) collectionViewLayout:self.layout];
        _listView.backgroundColor=KWhiteColor;
        _listView.delegate=self;
        _listView.dataSource = self;
        _listView.scrollEnabled = YES;
        [_listView registerClass:[SelecteStyleOfCookingItem class] forCellWithReuseIdentifier:NSStringFromClass(SelecteStyleOfCookingItem.class)];
        [_listView registerClass:[SelecteStyleOfCookingTextFileItem class] forCellWithReuseIdentifier:NSStringFromClass(SelecteStyleOfCookingTextFileItem.class)];

        [_listView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headReusableView"];
    }
    return _listView;
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        NSArray * array = @[@"川菜",@"粤菜",@"湘菜",@"东北菜",@"浙菜",@"鲁菜",@"淮扬菜",@"潮州菜",@"西北菜",@"徽菜",@"闽菜",@"苏菜",@"云南菜",@"新疆菜",@"晋菜",@"云南菜"];
        _dataArray =[[NSMutableArray alloc] init];
        [_dataArray addObjectsFromArray:array];
    }
    return _dataArray;
}

-(UIButton*)rightBtn{
    if(!_rightBtn){
        _rightBtn=[UIButton new];
        [_rightBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_rightBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateDisabled];
        [_rightBtn setTitleColor:UIColorFromRGB(0x009966) forState:UIControlStateNormal];
        _rightBtn.enabled = NO;
        [_rightBtn sizeToFit];
        kWeakSelf(weakSelf);
        [_rightBtn addActionHandler:^{
            if (!StrValid(weakSelf.selecteTitle)) {
                ShowHubMessage(@"请选择菜系");
                return ;
            }
            BlockSafeRun(weakSelf.callBlock,weakSelf.selecteTitle);
            [weakSelf backBtnClicked];
        }];
    }
    return _rightBtn;
}

@end


@interface SelecteStyleOfCookingItem ()
/**中间文字*/
@property (nonatomic,strong)UILabel*titlelabel;
@end

@implementation SelecteStyleOfCookingItem

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        ViewBorderRadius(self, 3, 1,  UIColorFromRGB(0xE9EEE7));
        
        UIView * selectView = UIView.new;
        selectView.backgroundColor = UIColorFromRGB(0xECFBF0);
        self.selectedBackgroundView =selectView;
        
        
        [self addSubview:self.titlelabel];
        
        [self.titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
        }];
        
    }
    return self;
}
-(void)setTitleName:(NSString *)titleName{
    _titleName = titleName;
    self.titlelabel.text = titleName;
}
-(void)setSelected:(BOOL)selected{
    if (selected) {
        self.titlelabel.textColor = UIColorFromRGB(0x009966);
    }else{
        self.titlelabel.textColor = KBlackColor;
    }
}
-(UILabel*)titlelabel{
    if(!_titlelabel){
        _titlelabel=[UILabel new];
        _titlelabel.textColor = KBlackColor;
        _titlelabel.font = SYSTEMFONT(14);
        _titlelabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titlelabel;
}
@end

@interface SelecteStyleOfCookingTextFileItem ()
/**输入框*/
@property (nonatomic,strong)UITextField*textField;
/**<#属性#>*/
@property (nonatomic,strong)UIButton*addBtn;
@end

@implementation SelecteStyleOfCookingTextFileItem

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        UIView * bgView = UIView.new;
        ViewBorderRadius(bgView, 3, 1, UIColorFromRGB(0xE9EEE7));
        
        [self addSubview:bgView];
        
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(16);
            make.right.mas_equalTo(-16);
            make.top.bottom.mas_equalTo(0);
        }];
        
        UIView * lineView = UIView.new;
        lineView.backgroundColor = UIColorFromRGB(0x959E97);
        
        UIButton * addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [addBtn setTitle:@"添加" forState:UIControlStateNormal];
        [addBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateNormal];
        
        
        [bgView addSubview:self.textField];
        [bgView addSubview:lineView];
        [bgView addSubview:addBtn];

        [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.mas_equalTo(16);
           make.right.mas_equalTo(-70);
           make.top.bottom.mas_equalTo(0);
        }];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.textField.mas_right);
            make.width.mas_equalTo(1);
            make.top.mas_equalTo(10);
            make.bottom.mas_equalTo(-10);
        }];
        
        [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-20);
        }];
        
    }
    return self;
}
-(UIButton*)addBtn{
    if(!_addBtn){
        _addBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        [_addBtn setTitle:@"添加" forState:UIControlStateNormal];
        [_addBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateDisabled];
        [_addBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateNormal];
        _addBtn.enabled = YES;
    }
    return _addBtn;
}
-(UITextField*)textField{
    if(!_textField){
        _textField=[UITextField new];
        _textField.placeholder = @"添加自定义菜系";
    }
    return _textField;
}
@end
